<?php

namespace app\api\controller;

use app\api\model\Eggs as EggsDB;
use Illuminate\Support\Carbon;
use support\Request;
use app\traits\GameCar;
use app\api\model\Wallets as WalletsDB;
use app\api\model\GameCar as GameCarDB;
use app\api\model\GameCarsPlayer as GameCarsPlayerDB;
use app\api\model\UserUseEggs as UserUseEggsDB;

//北京赛车游戏
class GameCarController
{
    use GameCar;

    public function add()
    {
        //添加一局游戏-十分钟一场-停止时间8分钟后，开奖时间9分钟后 期数为当前时间
        $req = [];
        $req['date_id']=date("YmdHi");
        $req['created_at']= Carbon::now()->toDateTimeString();
        $req['stop_time']= Carbon::now()->addMinutes(4)->toDateTimeString();
        $req['open_time']= Carbon::now()->addMinutes(5)->toDateTimeString();
        $res = GameCarDB::insertGetId($req);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }
    public function list()
    {
        $res = GameCarDB::orderBy('id', 'desc')->paginate(10);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }
    public function openGame()
    {
        $ranking = $this->ranking();
//        $ranking = [10,9,8,7,6,5,4,3,2,1];
//        //把开奖记录传给GameCarDB
        $game = GameCarDB::orderBy('id','desc')->first();
//        //更新游戏排名列表
        GameCarDB::where('date_id',$game['date_id'])->update([
            'ranking'=>json_encode($ranking)
        ]);
        GameCarsPlayerDB::where('date_id',$game['date_id'])->update(['is_open' => 1]);
        $res = $this->updateWinnerList($game['date_id'],$ranking);
        sleep(1);
        //获取所有投注信息
        $vs = GameCarsPlayerDB::where('date_id',$game['date_id'])->get()->toArray();
        foreach ($vs as $v){
            if($v['is_got']==1){
                //给钱包加上中奖金额和本金
                $held_eggs = WalletsDB::where('wallet_user_id',$v['user_id'])->value('held_eggs');
                WalletsDB::where('wallet_user_id',$v['user_id'])->update([
                    'held_eggs'=>$v['count']*$v['rate']+$held_eggs
                ]);
                //添加U蛋记录
                EggsDB::insertGetId([
                    'uid' => $v['user_id'],
                    'count' => $v['count']*$v['rate'],
                    'type' => '游戏-中奖-疯狂赛车'.$v['date_id'],
                    'yu_e'=>$v['count']*$v['rate']+$held_eggs,
                    'created_at' => Carbon::now()->toDateTimeString()
                ]);
            }
        }
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' =>[
                'ranking'=>$ranking,
                'list'=>$res
            ],
        ]);
    }
    function updateWinnerList($date_id,$ranking){
        $v = GameCarsPlayerDB::where('date_id',$date_id)->get()->toArray();
        $res =  $this->rank2type($ranking,$v,$date_id);
        GameCarsPlayerDB::where('date_id',$date_id)->upsert(
            $res,
            ['id'],
            ['is_got']
        );
        return $res;
    }
    //用户操作
    //获取当前游戏
    public function getNowGame(){
        $res = GameCarDB::orderBy('id','desc')->first();
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' =>$res,
        ]);
    }
    //押注
    public function playGame(Request $request)
    {
        $req = [];
        //获取用户id
        $uid = $request->uid;
        $req['user_id'] = $request->uid;
        $req['count'] = $request->post('count');
        $req['rate'] = $request->post('rate');
        $req['buy_type'] = $request->post('buy_type');
        $req['buy_car_id'] = $request->post('buy_car_id');
        $req['date_id'] = $request->post('date_id');
        $req['created_at'] = Carbon::now()->toDateTimeString();

        //判断是否暂停投注
        $r = GameCarDB::orderBy('id','desc')->first();
        $stop_time =Carbon::createFromFormat('Y-m-d H:i:s', $r['stop_time']);
        $now_time =  Carbon::now();
        if($now_time->gte($stop_time)){
            return json([
                'code' => 0,
                'msg' => 'ok',
                'data' => '已经暂停投注',
            ]);
        }
        //判断当前是否有count
        $count_db = WalletsDB::where('wallet_user_id', $uid)->value("held_eggs");
        if($req['count']<=$count_db){
            $res = GameCarsPlayerDB::insertGetId($req);
            WalletsDB::where('wallet_user_id', $uid)->update([
                "held_eggs"=>$count_db-$req['count'],
                "used_eggs"=> WalletsDB::where('wallet_user_id', $uid)->value("used_eggs")+$req['count']
            ]);
//            UserUseEggsDB::insertGetId([
//                'user_id'=>$uid,
//                'count'=>$req['count'],
//                'use_type'=>'游戏-赛车-投注',
//                'type'=>0,
//                'created_at'=> Carbon::now()->toDateTimeString()
//            ]);
            //添加U蛋记录
            EggsDB::insertGetId([
                'uid' =>$uid,
                'count' => $req['count'],
                'type' => '游戏-竞猜-疯狂赛车'.$req['date_id'],
                'yu_e'=>$count_db-$req['count'],
                'created_at' => Carbon::now()->toDateTimeString()
            ]);
        }else{
            return json([
                'code' => 0,
                'msg' => 'U蛋不够',
                'data' => '',
            ]);
        }
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }
    //开奖后兑奖
    public function test()
    {
//        //获取所有投注信息
//        $vs = GameCarsPlayerDB::where('date_id','202404151750')->get()->toArray();
//        foreach ($vs as $v){
//            if($v['is_got']==1){
//                //给钱包加上中奖金额和本金
//                $held_eggs = WalletsDB::where('wallet_user_id',$v['user_id'])->value('held_eggs');
//                WalletsDB::where('wallet_user_id',$v['user_id'])->update([
//                    'held_eggs'=>$v['count']*$v['rate']+$held_eggs
//                ]);
//                //添加U蛋记录
//                EggsDB::insertGetId([
//                    'uid' => $v['user_id'],
//                    'count' => $v['count']*$v['rate']+$v['count'],
//                    'type' => '游戏-中奖-疯狂赛车'.$v['date_id'],
//                    'yu_e'=>$v['count']*$v['rate']+$v['count']+$held_eggs,
//                    'created_at' => Carbon::now()->toDateTimeString()
//                ]);
//            }
//        }
//        return json($vs);
    }

    //兑奖
    public function gotPrize(Request $request)
    {
        $uid = $request->uid;
        $id = $request->post('id');
        $info = GameCarsPlayerDB::where('id',$id)->first();
        if($info['is_got']==1&&$info['is_dui']==0){
            //更新用户列表
            GameCarsPlayerDB::where('id',$id)->update([
                'is_dui'=>1
            ]);
            //更新用户钱包中奖额
            $held_eggs = WalletsDB::where('wallet_user_id',$uid)->value('held_eggs');
            $res = WalletsDB::where('wallet_user_id',$uid)->update([
                'held_eggs'=>$info['count']*$info['rate']+$held_eggs
            ]);
//            UserUseEggsDB::insertGetId([
//                'user_id'=>$uid,
//                'count'=>$info['count']*$info['rate'],
//                'use_type'=>'游戏-赛车-中奖',
//                'type'=>1,
//                'created_at'=> Carbon::now()->toDateTimeString()
//            ]);
            //添加U蛋记录
            EggsDB::insertGetId([
                'uid' =>$uid,
                'count' =>$info['count']*$info['rate']+$info['count'],
                'type' => '游戏-中奖-疯狂赛车'.$info['date_id'],
                'yu_e'=>$info['count']*$info['rate']+$held_eggs,
                'created_at' => Carbon::now()->toDateTimeString()
            ]);
            return json([
                'code' => 2,
                'msg' => 'ok',
                'data' => $res,
            ]);
        }else if($info['is_got']==1&&$info['is_dui']==1){
            return json([
                'code' => 4,
                'msg' => 'ok',
                'data' =>'已经兑过了',
            ]);
        } else{
            return json([
                'code' => 4,
                'msg' => 'ok',
                'data' =>'没有中奖',
            ]);
        }
    }
    //押注列表
    public function playGameList(Request $request)
    {
        $uid = $request->uid;
        $list = GameCarsPlayerDB::where('user_id',$uid)->orderBy('id', 'desc')->paginate(10);
        return json([
            'code' => 2,
            'msg' => 'ok',
            'data' => $list,
        ]);
    }

}
